package br.unisinos.ubicomp.locationawaretaskmanager;

import java.util.Date;

import com.google.android.maps.GeoPoint;

import android.location.Location;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TimePicker;
import android.widget.Toast;

public class TaskCreator extends Activity {

	private Button btHere;
	private Task task = new Task();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.task_creator_screen);

		// an adapter is linked to array and a layout
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.task_prio, android.R.layout.simple_spinner_item);
		// link the adapter to the spinner
		Spinner spinner_Prio = (Spinner) findViewById(R.id.spinner_Prio);
		spinner_Prio.setAdapter(adapter);

		this.btHere = (Button) findViewById(R.id.bt_Place_Here);
		this.btHere.setOnClickListener(new Button.OnClickListener() {
		    
			public void onClick(View v) {
				task.setLocation(getCurrentLocation());
		    }
			
		}); 
	}

	private GeoPoint getCurrentLocation() {
		Location currentLocation = MainTaskManager.getCurrentLocation();
	    double latitude;
	    double longitude;
	    
	    if(currentLocation != null) {
	    	latitude = currentLocation.getLatitude();
	    	longitude = currentLocation.getLongitude();
	    } else {
	    	latitude = -29.79469730177113;
	    	longitude =  -51.15378141403198;
	    }
	    
	    return new GeoPoint((int) (latitude * 1E6),
			                (int) (longitude * 1E6) );
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.task_creator_screen, menu);
		return true;
	}

	// Add new task button
	public void setPlace(View v) {
		Intent i = new Intent(getApplicationContext(), TaskLocationMaps.class);
		startActivityForResult(i, RequestCodes.PIN_POSITION);
	}

	// Ok Button
	public void Ok_Action(View v) {
		updateTask();
		Intent data = new Intent();
		data.putExtra("task", task);
		setResult(Activity.RESULT_OK, data);
		finish();
	}

	private void updateTask() {
		EditText edit_TaskName = (EditText) findViewById(R.id.editText_TaskName);
		DatePicker datePicker_task = (DatePicker) findViewById(R.id.datePicker_task);
		TimePicker timePicker_task = (TimePicker) findViewById(R.id.timePicker_task);
		Spinner spinner_Prio = (Spinner) findViewById(R.id.spinner_Prio);

		this.task.setName(edit_TaskName.getText().toString());

		this.task.setDate(new Date(datePicker_task.getYear() - 1900, 
								   datePicker_task.getMonth(), 
								   datePicker_task.getDayOfMonth(), 
								   timePicker_task.getCurrentHour(), 
								   timePicker_task.getCurrentMinute()));
	}

	// Cancel Button
	public void Cancel_Action(View v) {
		setResult(Activity.RESULT_CANCELED);
		finish();
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == RESULT_OK) {

			switch (requestCode) {
			case RequestCodes.PIN_POSITION:
				int[] location = data.getIntArrayExtra("result");

				GeoPoint gp = new GeoPoint(location[0], location[1]);
				this.task.setLocation(gp);

				break;

			default:
				break;
			}
		}
	}

}
